from game.ai.action import Action
from game.fov import *

class OpenAction(Action):
    def __init(self):
        Action.__init__(self)
        #closed door, open it

    def perform(self, dat, src, dst):
        #source tile
        s_tile = dat.level.tiles[src[0]][src[1]]
        if s_tile is None or s_tile.occupant is None:
            return None

        #print src, dst
        nx = src[0] + dst[0]
        ny = src[1] + dst[1]

        #destination tile
        d_tile = dat.level.tiles[nx][ny]
        if d_tile.doodad is not None:
            dd = d_tile.doodad
            dmeth = getattr(self, 'perform__{0}_{1}'.format(dd.name, dd.state.name), None)
            if dmeth is not None:
                dmeth(dat, dd, s_tile, d_tile)
        
        return None


    def perform__door_closed(self, dat, doodad, s_tile, d_tile):
        dat.res_mgr.load_doodad_state(doodad, 'open')
        dat.gui.msg_mgr.add_msg("You open a door. Cool. ")
        dat.level.reblit_background(d_tile.x, d_tile.y)
        if s_tile.occupant == dat.main_char:
            dat.level.apply_fog()
            do_fov(dat.level, dat.main_char.pos(), 10)

    def perform__door_locked(self, dat, doodad, s_tile, d_tile):
        dat.gui.msg_mgr.add_msg("You try open the door, but it is locked as, bro. ")

    def perform__door_open(self, dat, doodad, s_tile, d_tile):
        dat.gui.msg_mgr.add_msg("As you reach for the door, you have a sudden realisation: the door is already open. You ponder upon this enigma as the time passes, until a small voice in the back of your mind tells you that this is not the time to be spacing out. ")